package com.kc.business.service.impl;

import com.kc.business.domain.BizGoods;
import com.kc.business.domain.BizGoodsBuyback;
import com.kc.business.mapper.BizGoodsBuybackMapper;
import com.kc.business.mapper.BizGoodsMapper;
import com.kc.business.service.IBizGoodsBuybackService;
import com.kc.common.exception.CustomException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * TODO
 *
 * @author zhangshuo
 * @version 1.0
 * @date 2020/10/13
 */
@Service
public class BizGoodsBuybackServiceImpl implements IBizGoodsBuybackService {

    @Resource
    BizGoodsBuybackMapper goodsBuybackMapper;
    @Resource
    BizGoodsMapper goodsMapper;

    /**
     * 回购商品列表
     *
     * @param goodsBuyback
     * @return
     */
    @Override
    public List<BizGoodsBuyback> getList(BizGoodsBuyback goodsBuyback) {
        return goodsBuybackMapper.select(goodsBuyback);
    }

    /**
     * 新增回购商品
     *
     * @param goodsBuyback
     * @return
     */
    @Override
    public int add(BizGoodsBuyback goodsBuyback) {
        BizGoods goods = goodsMapper.selectByPrimaryKey(goodsBuyback.getProductId());
        if (goods == null) {
            throw new CustomException("无效的商品id");
        }
        if (0 == goods.getIsForty()) {
            throw new CustomException("非溯源商品");
        }
        goodsBuyback.setTitle(goods.getTitle());
        goodsBuyback.setThumb(goods.getThumb());
        goodsBuyback.setImgHeight(goods.getImgHeight());
        goodsBuyback.setImgWidth(goods.getImgWidth());
        return goodsBuybackMapper.insertSelective(goodsBuyback);
    }

    /**
     * 修改回购商品
     *
     * @param goodsBuyback
     * @return
     */
    @Override
    public int edit(BizGoodsBuyback goodsBuyback) {
        BizGoods goods = goodsMapper.selectByPrimaryKey(goodsBuyback.getProductId());
        if (goods == null) {
            throw new CustomException("无效的商品id");
        }
        if (0 == goods.getIsForty()) {
            throw new CustomException("非溯源商品");
        }
        goodsBuyback.setTitle(goods.getTitle());
        goodsBuyback.setThumb(goods.getThumb());
        return goodsBuybackMapper.updateByPrimaryKeySelective(goodsBuyback);
    }

    /**
     * 删除回购商品
     *
     * @param goodsBuyback
     * @return
     */
    @Override
    public int delete(BizGoodsBuyback goodsBuyback) {
        return goodsBuybackMapper.updateByPrimaryKeySelective(goodsBuyback);
    }
}
